<template>
  <!-- 文件管理弹窗 -->
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false" width="600px">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="80px">
      <el-form-item prop="fileName" label="文件名">
        <el-input v-model.trim="dataForm.fileName" placeholder="文件名"></el-input>
      </el-form-item>
      <el-form-item prop="url" label="上传文件">
        <div class="updateImg-box">
          <updateFile ref="uploadRef" :urlList="dataForm.urlList" @updatList="updatList"></updateFile>
        </div>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref, nextTick } from "vue";
import baseService from "@/service/baseService";
import { IObject } from "@/types/interface";
import { ElMessage } from "element-plus";
import updateFile from "@/components/updateFile.vue";
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();
const uploadRef = ref();

const dataForm = reactive<IObject>({
  id: "",
  fileName: "",
  fileSize: "",
  url: "",
  urlList: []
});

const rules = ref({
  fileName: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  url: [{ required: true, message: "必填项不能为空", trigger: "blur" }]
});

const init = async (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataForm.urlList = [];
    dataFormRef.value.resetFields();
  }

  if (id) {
    await getInfo(id);
  }

  await nextTick(() => {
    uploadRef.value.init();
  });
};

// 获取信息
const getInfo = async (id: number) => {
  await baseService.get(`/lingyao/fileinfo/${id}`).then((res) => {
    console.log("获取信息", res.data);
    if (res.data.url) {
      res.data.urlList = [
        {
          name: res.data.fileName,
          size: res.data.fileSize,
          response: {
            code: 0,
            data: { src: res.data.url }
          },
          uid: res.data.id
        }
      ];
    } else {
      res.data.urlList = [];
    }
    Object.assign(dataForm, res.data);
  });
};

// 上传文件成功
const updatList = (list: IObject[]) => {
  if (list.length > 0) {
    dataForm.fileSize = list[0].size;
    dataForm.url = list[0].response.data.src;
  } else {
    dataForm.fileSize = null;
    dataForm.url = "";
  }
  dataForm.urlList = list;
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/lingyao/fileinfo", dataForm).then((res) => {
      ElMessage.success({
        message: "成功",
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>

<style lang="scss" scoped>
.updateImg-box {
  width: 100%;
}
</style>